import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [{
    path: "/",
    name: "login",
    component: () => import("../views/Login.vue")
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/Index.vue")
  },

  {
    path: "/me",
    name: "me",
    component: () => import("../views/Mine.vue")
  },
  {
    path: "/admin",
    name: "admin",
    component: () => import("../views/Admin.vue"),
    redirect: "/usermanage",
    children: [{
        path: "/usermanage",
        name: "usermanage",
        component: () => import("../views/admin/Usermanage.vue")
      },
      {
        path: "/tagmanage",
        name: "tagmanage",
        component: () => import("../views/admin/Tagmanage.vue")
      },
      {
        path: "/resourcemanage",
        name: "resourcemanage",
        component: () => import("../views/admin/Resourcemanage.vue")
      },
      {
        path: "/operatemanage",
        name: "operatemanage",
        component: () => import("../views/admin/Operatemanage.vue")
      }
    ]
  }
];

const router = new VueRouter({
  routes
});
// 设置路由的前置守卫（路由跳转之前）,必须登录才能跳转其他页面的路由，sessionStorage关闭浏览器后失效
router.beforeEach((to, from, next) => {
  if (to.name == "login") {
    next();
  } else {
    // 判断sesstion里有没有username
    const username = sessionStorage.getItem("username");
    if (username) {
      next();
    } else {
      next({
        path: "/"
      });
    }
  }
});

export default router;
